package com.ostrich.note.ui.note.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseMvvmActivity;
import com.ostrich.note.databinding.ActivityTypeEditBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.ui.note.adapter.NoteTypeEditAdapter;
import com.ostrich.note.ui.note.entity.TypeEntity;
import com.ostrich.note.ui.note.viewmodel.NoteTypeViewModel;
import com.ostrich.note.utils.DialogUtils;

public class TypeEditActivity extends BaseMvvmActivity<ActivityTypeEditBinding, NoteTypeViewModel>{
    private NoteTypeEditAdapter mNoteTypeEditAdapter;
    private  TypeEntity data;
    @Override
    protected void iniView() {
        mNoteTypeEditAdapter = new NoteTypeEditAdapter();
        binding.rv.setAdapter(mNoteTypeEditAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    public int getLayout() {
        return R.layout.activity_type_edit;
    }

    @Override
    public void initData() {
        data = (TypeEntity) getBundle().getSerializable("data");
        mNoteTypeEditAdapter.setData(data.getList());

    }

    @Override
    public void initListener() {
        binding.clBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mNoteTypeEditAdapter.setItemChildClickListener(new NoteTypeEditAdapter.ItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                TypeEntity.ListBean data = mNoteTypeEditAdapter.getDataByPosition(position);
                if(view.getId() == R.id.iv_sub){
                    DialogUtils.showTypeDeleteDialog(TypeEditActivity.this, data.getTitle(), new DialogUtils.DialogListener() {
                        @Override
                        public void onCancelClick() {

                        }

                        @Override
                        public void onConfirmClick() {
                            //删除分类
                            viewModel.notepadClassDel(data.getNotepad_id()).observe(TypeEditActivity.this, new Observer<BaseResponseEntity>() {
                                @Override
                                public void onChanged(BaseResponseEntity baseResponseEntity) {
                                    int code = baseResponseEntity.getCode();
                                    if(code == 200){
                                        Toast.makeText(TypeEditActivity.this, baseResponseEntity.getMessage(), Toast.LENGTH_SHORT).show();
                                        mNoteTypeEditAdapter.deleteData(position);
                                    }
                                }
                            });
                        }

                        @Override
                        public void onConfirmClick(String data) {

                        }

                        @Override
                        public void onCheckChanged(boolean isChecked) {

                        }
                    });
                }else if(view.getId() == R.id.iv_edit){
                    DialogUtils.showTypeEditDialog(TypeEditActivity.this, data.getTitle(), new DialogUtils.DialogListener() {
                        @Override
                        public void onCancelClick() {

                        }

                        @Override
                        public void onConfirmClick() {

                        }

                        @Override
                        public void onConfirmClick(String title) {
                            //修改分类
                            viewModel.notepadTitleEdit(title,000000,data.getNotepad_id()).observe(TypeEditActivity.this, new Observer<BaseResponseEntity>() {
                                @Override
                                public void onChanged(BaseResponseEntity baseResponseEntity) {
                                    if(baseResponseEntity.getCode() == 200){
                                        ToastUtils.showShort(baseResponseEntity.getMessage());
                                        mNoteTypeEditAdapter.updateData(position,title);
                                    }



                                }
                            });
                        }

                        @Override
                        public void onCheckChanged(boolean isChecked) {

                        }
                    });
                }

            }
        });
        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.showTypeInsertDialog(TypeEditActivity.this, new DialogUtils.DialogListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onConfirmClick() {

                    }

                    @Override
                    public void onConfirmClick(String data) {
                        //用户输入的修改的数据
                        viewModel.insertNotepadType(data,345693).observe(TypeEditActivity.this, new Observer<BaseResponseEntity>() {
                            @Override
                            public void onChanged(BaseResponseEntity baseResponseEntity) {
                                if(baseResponseEntity.getCode() == 200){
                                    ToastUtils.showShort(baseResponseEntity.getMessage());
                                    TypeEntity.ListBean listBean = new TypeEntity.ListBean();
                                    listBean.setNum(0);
                                    listBean.setTitle(data);
                                    mNoteTypeEditAdapter.insertData(listBean);

                                }
                            }
                        });






                    }

                    @Override
                    public void onCheckChanged(boolean isChecked) {

                    }
                });
            }
        });
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(NoteTypeViewModel.class);
    }
}